package cn.kawa.order.service.impl;

import cn.kawa.basic.util.PageList;
import cn.kawa.order.domain.OrderPetAcquisition;
import cn.kawa.order.dto.ConfirmDto;
import cn.kawa.order.mapper.OrderPetAcquisitionMapper;
import cn.kawa.order.query.OrderPetAcquisitionQuery;
import cn.kawa.order.service.IOrderPetAcquisitionService;
import cn.kawa.basic.service.impl.BaseServiceImpl;
import cn.kawa.org.domain.Employee;
import cn.kawa.org.domain.Shop;
import cn.kawa.org.mapper.EmployeeMapper;
import cn.kawa.org.mapper.ShopMapper;
import cn.kawa.pet.domain.Pet;
import cn.kawa.pet.mapper.PetMapper;
import cn.kawa.searchMsg.domain.SearchMasterMsg;
import cn.kawa.searchMsg.mapper.SearchMasterMsgMapper;
import cn.kawa.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;


    /**
     * 根据页面传的 LoginInfo 来拿出empID shopId判断权限
     * @param query
     * @param loginUser
     * @return
     */
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, LoginInfo loginUser) {
        //1.根据LoginInfo查询emp和shop，判断员工是哪种角色
        Employee employee = employeeMapper.loadByLogininfoId(loginUser.getId());
        Long shopId = employee.getShop_id();
        if(shopId!=null){//2.店铺id不为空，首先判断不是超管,要把shopId放入query
            query.setShopId(shopId);
            //System.out.println("empId"+employee.getId());//324
            Shop shop = shopMapper.loadById(shopId);
            if(!shop.getAdmin_id().equals(employee.getId())){//2.判断不是店铺管理的话，把empId放如query
                query.setEmpId(employee.getId());
            }
        }
        //query.setEmpId(324L);
        //query.setShopId(39L);
        return super.queryPage(query);
    }

    /**
     * 1.改订单状态，金额
     * 2.改寻主状态
     * 3.更改pet表金额
     * 注意寻主信息对象，pet对象，都从订单中获取
     * @param confirmDto
     */
    @Override
    public void confirm(ConfirmDto confirmDto) {
        Long id = confirmDto.getId();//id : orderId
        BigDecimal money = confirmDto.getMoney();
        Integer payType = confirmDto.getPayType();
        //1.改订单状态，金额，支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //2.改寻主状态 : 通过订单表的getSearchMasterMsgId bean 来获取 smmId后， 用mapper来获取寻主信息对象
        Long searchMasterMsgId = orderPetAcquisition.getSearchMasterMsgId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(searchMasterMsgId);
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
        //3.更改pet表金额  :  从订单对象里获取petId
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);

    }

    /** 重构删除的接口用于取消订单
     *  1.修改订单状态
     *  2.寻主消息 shopId = null
     *  3.删除pet
     *  注意寻主信息对象，pet对象，都从订单中获取
     * @param id : orderPetAcquisition.id
     */
    @Override
    public void cancel(Long id) {
        //1.修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //2.寻主消息 shopId = null  :将寻主消息放回寻主池
        Long searchMasterMsgId = orderPetAcquisition.getSearchMasterMsgId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(searchMasterMsgId);
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        //3.删除pet
        Long petId = orderPetAcquisition.getPetId();
        petMapper.remove(petId);
    }
}
